package com.manage.project.biz.service.impl;

import com.manage.project.biz.domain.BizJishupingding;
import com.manage.project.biz.domain.CivilEnginneeringTechnicalTotal;
import com.manage.project.biz.domain.TechnicalTotal;
import com.manage.project.biz.mapper.TechnicalTotalMapper;
import com.manage.project.biz.service.IBizJishupingdingService;
import com.manage.project.biz.service.ICivilEnginneeringTechnicalTotalService;
import com.manage.project.biz.service.ITechnicalTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 总体技术状况评定Service业务层处理
 *
 * @author 小赵
 * @date 2022-05-22
 */
@Service
public class TechnicalTotalServiceImpl implements ITechnicalTotalService
{
    @Autowired
    private TechnicalTotalMapper technicalTotalMapper;
    @Autowired
    private ICivilEnginneeringTechnicalTotalService iCivilEnginneeringTechnicalTotalService;
    @Autowired
    private IBizJishupingdingService jishupingdingService;

    @Override
    public TechnicalTotal getByTotalId(Long totalId) {
        return technicalTotalMapper.getById(totalId);
    }

    /**
     * 查询总体技术状况评定
     *
     * @param totalId 总体技术状况评定主键
     * @return 总体技术状况评定
     */
    @Override
    public TechnicalTotal selectTechnicalTotalByTotalId(Long totalId)
    {
        return technicalTotalMapper.selectTechnicalTotalByTotalId(totalId);
    }

    /**
     * 查询总体技术状况评定列表
     *
     * @param technicalTotal 总体技术状况评定
     * @return 总体技术状况评定
     */
    @Override
    public List<TechnicalTotal> selectTechnicalTotalList(TechnicalTotal technicalTotal)
    {
        return technicalTotalMapper.selectTechnicalTotalList(technicalTotal);
    }

    /**
     * 新增总体技术状况评定
     *
     * @param technicalTotal 总体技术状况评定
     * @return 结果
     */
    @Override
    public int insertTechnicalTotal(TechnicalTotal technicalTotal)
    {
        String tunnelName=technicalTotal.getTunnelName();
        Date adjustDate=technicalTotal.getAdjustData();
        SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= strdate.format(adjustDate);
        CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal=
                iCivilEnginneeringTechnicalTotalService.selectByTunnelNameAndDate(tunnelName,adjustDate);
        BizJishupingding bizJishupingding=jishupingdingService.selectByTunnelNameAndDate(tunnelName,strDate);
        if(civilEnginneeringTechnicalTotal!=null){
            technicalTotal.setDirectLimit(civilEnginneeringTechnicalTotal.getDirectLimit());
            technicalTotal.setTopLimit(civilEnginneeringTechnicalTotal.getTopLimit());
            technicalTotal.setTunnelDoorLimit(civilEnginneeringTechnicalTotal.getTunnelDoorLimit());
            technicalTotal.setTunnelHoleLimit(civilEnginneeringTechnicalTotal.getTunnelHoleLimit());
            technicalTotal.setLinningLimit(civilEnginneeringTechnicalTotal.getLinningLimit());
            technicalTotal.setRoadsurfaceLimit(civilEnginneeringTechnicalTotal.getRoadsurfaceLimit());
            technicalTotal.setJianxiudaoLimit(civilEnginneeringTechnicalTotal.getJianxiudaoLimit());
            technicalTotal.setWaterLimit(civilEnginneeringTechnicalTotal.getWaterLimit());
            technicalTotal.setMarkingLimit(civilEnginneeringTechnicalTotal.getMarkingLimit());

            technicalTotal.setDirectMax(civilEnginneeringTechnicalTotal.getDirectMax());
            technicalTotal.setTopMax(civilEnginneeringTechnicalTotal.getTopMax());
            technicalTotal.setTunnelDoorMax(civilEnginneeringTechnicalTotal.getTunnelDoorMax());
            technicalTotal.setTunnelHoleMax(civilEnginneeringTechnicalTotal.getTunnelHoleMax());
            technicalTotal.setLiningMax(civilEnginneeringTechnicalTotal.getLiningMax());
            technicalTotal.setRoadsurfaceMax(civilEnginneeringTechnicalTotal.getRoadsurfaceMax());
            technicalTotal.setJianxiudaoMax(civilEnginneeringTechnicalTotal.getJianxiudaoMax());
            technicalTotal.setWaterMax(civilEnginneeringTechnicalTotal.getWaterMax());
            technicalTotal.setMarkingMax(civilEnginneeringTechnicalTotal.getMarkingMax());

            technicalTotal.setCivilGrade(civilEnginneeringTechnicalTotal.getGrade());
            technicalTotal.setCivilPclass(civilEnginneeringTechnicalTotal.getPclass());
        }
        if(bizJishupingding!=null){
            int jdGrade=Integer.parseInt(bizJishupingding.getField7());
            technicalTotal.setJdGrade(jdGrade);
            technicalTotal.setJdPclass(bizJishupingding.getField8());
        }
        long JDCI=Integer.parseInt(bizJishupingding.getField7());
        long JGCI=civilEnginneeringTechnicalTotal.getGrade();
        if(JDCI>JGCI){
            technicalTotal.setFinalGrade(JGCI);
            technicalTotal.setFinalPclass(civilEnginneeringTechnicalTotal.getPclass());
            technicalTotal.setSuggest(civilEnginneeringTechnicalTotal.getSuggest());
        }else {
            technicalTotal.setFinalGrade(JDCI);
            technicalTotal.setFinalPclass(bizJishupingding.getField8());
            technicalTotal.setSuggest(bizJishupingding.getField9());
        }
        return technicalTotalMapper.insertTechnicalTotal(technicalTotal);
    }

    /**
     * 修改总体技术状况评定
     *
     * @param technicalTotal 总体技术状况评定
     * @return 结果
     */
    @Override
    public int updateTechnicalTotal(TechnicalTotal technicalTotal)
    {
        String tunnelName=technicalTotal.getTunnelName();
        Date adjustDate=technicalTotal.getAdjustData();
        SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= strdate.format(adjustDate);
        CivilEnginneeringTechnicalTotal civilEnginneeringTechnicalTotal=
                iCivilEnginneeringTechnicalTotalService.selectByTunnelNameAndDate(tunnelName,adjustDate);
        BizJishupingding bizJishupingding=jishupingdingService.selectByTunnelNameAndDate(tunnelName,strDate);
        if(civilEnginneeringTechnicalTotal!=null){
            technicalTotal.setDirectLimit(civilEnginneeringTechnicalTotal.getDirectLimit());
            technicalTotal.setTopLimit(civilEnginneeringTechnicalTotal.getTopLimit());
            technicalTotal.setTunnelDoorLimit(civilEnginneeringTechnicalTotal.getTunnelDoorLimit());
            technicalTotal.setTunnelHoleLimit(civilEnginneeringTechnicalTotal.getTunnelHoleLimit());
            technicalTotal.setLinningLimit(civilEnginneeringTechnicalTotal.getLinningLimit());
            technicalTotal.setRoadsurfaceLimit(civilEnginneeringTechnicalTotal.getRoadsurfaceLimit());
            technicalTotal.setJianxiudaoLimit(civilEnginneeringTechnicalTotal.getJianxiudaoLimit());
            technicalTotal.setWaterLimit(civilEnginneeringTechnicalTotal.getWaterLimit());
            technicalTotal.setMarkingLimit(civilEnginneeringTechnicalTotal.getMarkingLimit());

            technicalTotal.setDirectMax(civilEnginneeringTechnicalTotal.getDirectMax());
            technicalTotal.setTopMax(civilEnginneeringTechnicalTotal.getTopMax());
            technicalTotal.setTunnelDoorMax(civilEnginneeringTechnicalTotal.getTunnelDoorMax());
            technicalTotal.setTunnelHoleMax(civilEnginneeringTechnicalTotal.getTunnelHoleMax());
            technicalTotal.setLiningMax(civilEnginneeringTechnicalTotal.getLiningMax());
            technicalTotal.setRoadsurfaceMax(civilEnginneeringTechnicalTotal.getRoadsurfaceMax());
            technicalTotal.setJianxiudaoMax(civilEnginneeringTechnicalTotal.getJianxiudaoMax());
            technicalTotal.setWaterMax(civilEnginneeringTechnicalTotal.getWaterMax());
            technicalTotal.setMarkingMax(civilEnginneeringTechnicalTotal.getMarkingMax());

            technicalTotal.setCivilGrade(civilEnginneeringTechnicalTotal.getGrade());
            technicalTotal.setCivilPclass(civilEnginneeringTechnicalTotal.getPclass());
        }
        if(bizJishupingding!=null){
            int jdGrade=Integer.parseInt(bizJishupingding.getField7());
            technicalTotal.setJdGrade(jdGrade);
            technicalTotal.setJdPclass(bizJishupingding.getField8());
        }
        long JDCI=Integer.parseInt(bizJishupingding.getField7());
        long JGCI=civilEnginneeringTechnicalTotal.getGrade();
        if(JDCI>JGCI){
            technicalTotal.setFinalGrade(JGCI);
            technicalTotal.setFinalPclass(civilEnginneeringTechnicalTotal.getPclass());
            technicalTotal.setSuggest(civilEnginneeringTechnicalTotal.getSuggest());
        }else {
            technicalTotal.setFinalGrade(JDCI);
            technicalTotal.setFinalPclass(bizJishupingding.getField8());
            technicalTotal.setSuggest(bizJishupingding.getField9());
        }
        return technicalTotalMapper.updateTechnicalTotal(technicalTotal);
    }

    /**
     * 批量删除总体技术状况评定
     *
     * @param totalIds 需要删除的总体技术状况评定主键
     * @return 结果
     */
    @Override
    public int deleteTechnicalTotalByTotalIds(Long[] totalIds)
    {
        return technicalTotalMapper.deleteTechnicalTotalByTotalIds(totalIds);
    }

    /**
     * 删除总体技术状况评定信息
     *
     * @param totalId 总体技术状况评定主键
     * @return 结果
     */
    @Override
    public int deleteTechnicalTotalByTotalId(Long totalId)
    {
        return technicalTotalMapper.deleteTechnicalTotalByTotalId(totalId);
    }
}
